// <copyright file="EndpointValidationProviderManager.cs" company="SharpSTS">
// Copyright (c) 2007, 2008 All Right Reserved, http://sharpsts.com/
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Barry Dorrans</author>
// <email>barryd@idunno.org</email>
// <date>2008-06-14</date>
// <summary>Provides access to the EndpointValidationProvider.</summary>

namespace SharpSTS.Providers
{
    using System.Collections.ObjectModel;
    using System.Configuration;
    using System.Globalization;
    using System.ServiceModel;
    using System.Web.Configuration;    
    
    using Configuration;

    /// <summary>
    /// The entry point to the provider model for validating endpoints.
    /// </summary>
    internal static class EndpointValidationProviderManager
    {
        /// <summary>
        /// Provides a syncronisation object for thread safe provider loading.
        /// </summary>
        private static readonly object syncRoot = new object();

        /// <summary>
        /// The selected <see cref="EndpointValidationProvider"/>.
        /// </summary>
        private static EndpointValidationProvider provider;

        /// <summary>
        /// Gets the provider.
        /// </summary>
        /// <value>The provider.</value>
        private static EndpointValidationProvider Provider
        {
            get
            {
                LoadProviders();
                return provider;
            }
        }

        /// <summary>
        /// Validates that an endpoint address may request the list of provided claims.
        /// </summary>
        /// <param name="endpoint">The endpoint address to validate.</param>
        /// <param name="claims">The list of claim URIs to validate.</param>
        /// <returns>True if the endpoint is valid, otherwise false.</returns>
        public static bool IsValid(EndpointAddress endpoint, ReadOnlyCollection<string> claims)
        {
            return Provider.IsValid(endpoint, claims);
        }

        /// <summary>
        /// Initializes this instance of the <see cref="EndpointValidationProvider"/>.
        /// </summary>
        private static void LoadProviders()
        {
            if (null != provider)
            {
                return;
            }

            lock (syncRoot)
            {
                if (null != provider)
                {
                    return;
                }

                string providerSectionPath = 
                    string.Format(
                        CultureInfo.InvariantCulture, 
                        "{0}/{1}",
                        ProvidersSectionGroup.ConfigurationSectionGroupName,
                        EndpointValidationProviderSection.ConfigurationSectionName);
                EndpointValidationProviderSection section =
                    (EndpointValidationProviderSection)
                    ConfigurationManager.GetSection(providerSectionPath);

                EndpointValidationProviderCollection providers = new EndpointValidationProviderCollection();
                ProvidersHelper.InstantiateProviders(section.Providers, providers, typeof(EndpointValidationProvider));
                provider = providers[section.DefaultProvider];
            }
        }
    }
}
